$(function () {
    var index = null;
    var layer = null;
    var laypage = null;
    layui.use(['layer'], function () {
        layer = layui.layer;
    })
    //制作分页条 ----1.获取数据的总数  2.渲染分页条
    getUserCountData();
    $("#check").click(function () {
        getUserCountData();
    })
    requestAjax("../indent/count.htm", "get", "JSON", "", function (data) {
        $("#count").text(data);
    })
})
//1.获取数据库记录的总数
function getUserCountData() {
    var order_time = $("#test1").val();
    var see_time = $("#test2").val();
    var hospital_id = $("#typeid option:selected").val();
    if (hospital_id != null) {
        hospital_id=hospital_id;
    } else {
        hospital_id = 0;
    }
    var indent_number = $("input[name=\"indent_number\"]").val();
    requestAjax("../indent/getPagingCount.htm", "get", "JSON", "indent_number=" + indent_number+"&hospital_id="+hospital_id+"&order_time="+order_time+"&see_time="+see_time, function (data) {
        if (data.result) {
            //制作分页条
             makePageIndexByCount(data.data);
        } else {
            layer.msg(data.msg);
        }
    })
}

//制作分页条  ---入参 就是 总数
function makePageIndexByCount(data) {
    var laypage = layui.laypage;
    layui.use(['layer', "laypage"], function () {
        var laypage = layui.laypage;
        //完整功能
        laypage.render({
            elem: "page",
            limit: 10,
            count: data, //数据总数
            //自定义排版。可选值有：count（总条目输区域）、prev（上一页区域）、page（分页区域）、next（下一页区域）、limit（条目选项区域）、refresh（页面刷新区域。注意：layui 2.3.0 新增） 、skip（快捷跳页区域）
            layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
            , jump: function (obj, first) {
                //obj包含了当前分页的所有参数，比如：
                //调用后端接口请求数据，----》数据回来后通过jquery将数据放在页面上.
                 pageData(obj.curr, obj.limit);
                //首次不执行
                if (!first) {
                    //do something
                }
            }
        });
    })
}

//发送请求获取分页数据
function pageData(curr, limit) {
    var order_time = $("#test1").val();
    var see_time = $("#test2").val();
    var hospital_id = $("#typeid option:selected").val();
    if (hospital_id != null) {
        hospital_id=hospital_id;
    } else {
        hospital_id = 0;
    }
    var indent_number = $("input[name=\"indent_number\"]").val();
    requestAjax("../indent/showindent.htm", "get", "JSON", "indent_number=" + indent_number+"&PageIndex=" + curr + "&pageSize=" + limit+"&hospital_id="+hospital_id+"&order_time="+order_time+"&see_time="+see_time, function (data) {
        if (data.result) {
            // var all = JSON.stringify(data.data);
            // console.log(all);
             renderPageData(data.data);
        } else {
            layer.msg(data.msg);
        }
    })
}

function renderPageData(indentlist) {
    //获取tr模板
    var $tr_template = $(".providerTable tr:last");
    var $table = $(".providerTable");
    $(".providerTable tr:gt(0)").remove();
    // userlist传进来的是对象数组
    for (var i = 0; i < indentlist.length; i++) {
        var $tr = $tr_template.clone();
        $tr.find("#indent_number").text(indentlist[i].indent_number);
        $tr.find("#user_name").text(indentlist[i].user.user_name);
        $tr.find("#user_phone").text(indentlist[i].user.user_phone);
        $tr.find("#hospital_name").text(indentlist[i].hospital.hospital_name);
        $tr.find("#doctor_name").text(indentlist[i].doctor.doctor_name);
        $tr.find("#order_time").text(indentlist[i].order_time);
        $tr.find("#see_time").text(indentlist[i].see_time);
        $tr.find(".asdate").attr("id", indentlist[i].indent_id);
        $table.append($tr);
    }
    $tr_template.remove();
}